<?php

/**
 * This is the model class for table "procedures".
 *
 * The followings are the available columns in table 'procedures':
 * @property string $id
 * @property string $name
 * @property string $database_id
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property Databases $database
 */
class Procedures extends JLActiveRecord
{
	public $issue = null;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Procedures the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'procedures';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, database_id', 'required'),
			array('status', 'numerical', 'integerOnly'=>true),
			array('id', 'length', 'max'=>16),
			array('name', 'length', 'max'=>200),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, database_id, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'database' => array(self::BELONGS_TO, 'Databases', 'database_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'database_id' => 'Database',
			'status' => 'Status',
			'created' => 'Received',
			'issue' => 'Issue',
		);
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		
		if($this->database_id != null){
			$db = Databases::model()->findByAttributes(array('name'=>$this->database_id));
			if(!empty($db)) $this->database_id = $db->id;
		}
		$criteria->compare('id',$this->id,false);
		$criteria->compare('name',$this->name,false);
		$criteria->compare('database_id',$this->database_id,false);
		$criteria->compare('status',$this->status,false);

		return new CActiveDataProvider($this, array(
			'pagination'=>array(
				'pageSize'=>50,
			),
			'criteria'=>$criteria,
		));
	}
}